package view;
import server.ServerMenuController;
import model.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ServerView extends JFrame {
    private int framePositionX;
    private int framePositionY;
    private JMenu menu = null;
    private Rectangle frameBounds = null;
    private static JTextArea statusField = new JTextArea(5,0);

    private static ServerView server;

    public ServerView() {
        super("Server");
        setSize(650,450);

        //content pane
        //components spaced 10pixels apart
        JPanel contentPane = new JPanel(new BorderLayout(0, 10));
        setContentPane(contentPane);

        //space around the content pane
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        //setup menubar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuSetup(menuBar);

        //scrollpane for the status text field
        JScrollPane statusScrPane = new JScrollPane(statusField);
        statusScrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        statusScrPane.setBorder(BorderFactory.createLoweredBevelBorder());

        // set status field properties
        statusField.setEditable(false);
        statusField.setLineWrap(true);
        statusField.setWrapStyleWord(true);

        // add the panes to the content pane);
        contentPane.add(statusScrPane);

        // center the main window
        Dimension screenSize = getToolkit().getScreenSize();
        frameBounds = getBounds();
        framePositionX = (screenSize.width - frameBounds.width)/2;
        framePositionY = (screenSize.height - frameBounds.height)/2;
        setLocation(framePositionX, framePositionY);

        //sets the window to visible
        setVisible(true);
        //registers the controllers
        registerControllers();

        // anonymous inner class to terminate program when window is closed
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

     /**
     * Places the given window approximately at the center of the screen
     *
     * @param w    window to be centered, can be used on other windows
     */
    public void centerWindow(Window w) {
        Rectangle winBounds = w.getBounds();
        w.setLocation(framePositionX + (frameBounds.width - winBounds.width)/2,
		      framePositionY + (frameBounds.height - winBounds.height)/2);
    }
     /**
     * Adds the given string to the status text area
     *
     * @param s    string added to text area
     */
    public void updateStatusBar(String s) {
        // trim() removes whitespace and control characters at both ends of the string
        statusField.append(s.trim() + "\n");

        // This informs the scroll pane to update itself and its scroll bars.
        // The scroll pane does not always automatically scroll to the message that was
        // just added to the text area. This line guarantees that it does.
        statusField.revalidate();
    }

        /**
     * Creates a menu and adds items in a list
     *
     * @param mb    the name of the menu bar used for its location
     */
    private void menuSetup(JMenuBar mb) {

        //first menu
        menu = new JMenu("Start");
        menu.setMnemonic(KeyEvent.VK_S);

        //menu items
        createMenuItem(menu, "New Client...",
		       KeyEvent.VK_N, "NewClient");
        createMenuItem(menu, "Exit...",
		       KeyEvent.VK_E, "Exit");
        mb.add(menu);
    }

    /**
     * Creates a menu item and adds it to the given menu.
     *
     * @param menu      the menu name the item is associated with
     * @param label     name that appears in the menu view
     * @param nmonic    Key shortcut key used to access menu item
     * @param action    Command command name to run in MenuController
     */
    private JMenuItem createMenuItem(JMenu menu, String label, int mnemonicKey, String actionCommand) {
        JMenuItem menuItem = new JMenuItem(label);

        if (mnemonicKey > 0) {
            menuItem.setMnemonic(mnemonicKey);
        }

        if (actionCommand.length() > 0) {
            menuItem.setActionCommand(actionCommand);
        }

        menu.add(menuItem);
        return menuItem;
    }

        /**
     * registers the controllers for all items in each menu. This
     * method should only be executed once.
     */
    public void registerControllers() {
        JMenuItem menuItem;

        // MenuController handles events on menu items (i.e. when they are clicked)
        ServerMenuController sm = new ServerMenuController(this);

        for (int i = 0; i < menu.getItemCount(); i++) {
            menuItem = menu.getItem(i);
            menuItem.addActionListener(sm);
        }
    }

     /**
     * Prints a message in the statusField
     *
     * @param message   message to be printed
     */
    public static void printMessage(String message) {
        String mess = message;
        statusField.append(mess);
    }

    public static ServerView getInstance() {
        if (server == null) {
            server = new ServerView();
        }
        return server;
    }
}
